import logging

from config import BASE_HOST


class RegLoginApi:

    def __init__(self,ses):
        '''

        :param ses: session对象
        '''
        self.ses = ses

    # 获取图片验证码接口
    def get_img_verify_code(self, r):
        '''

        :param r: 为随机小数，或者随机整数，12位
        :return:
        '''
        url = BASE_HOST+f"/common/public/verifycode1/{r}"
        resp = self.ses.get(url)
        logging.info(f'获取图片验证码响应状态码为{resp.status_code}')
        return  resp

    # 获取短信验证码接口
    def get_phone_verify_code(self, phone_v, img_code_v='8888', type_v='reg'):
        '''
        获取短信验证码
        :param phone_v: 电话号码
        :param img_code_v: 图片验证码
        :param type_v:
        :return: 返回响应结果
        '''
        body = {"phone": phone_v,
                "imgVerifyCode": img_code_v,
                "type": type_v}
        url = BASE_HOST+"/member/public/sendSms"
        resp = self.ses.post(url,data=body)
        logging.info(f'获取短信验证码接口的响应体为 {resp.json()}')
        return resp

    # 注册接口
    def user_register(self, form_dict):
        '''
        注册接口
        :param form_dict:注册接口的请求体
        :return:
        '''
        url = BASE_HOST+"/member/public/reg"
        resp = self.ses.post(url,data=form_dict)
        logging.info(f'注册接口的响应体为 {resp.json()}')
        return resp


    # 登录接口
    def user_login(self, username_v, pwd_v):
        '''
        登录接口的封装
        :param username_v: 账号
        :param pwd_v: 密码
        :return:
        '''
        url = BASE_HOST+"/member/public/login"
        body = {"keywords":username_v,"password":pwd_v}
        resp = self.ses.post(url,data=body)
        logging.info(f'登录接口的响应体{resp.json()}')
        return resp

